<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* .box{
            position: relative;
        } */
        .header{
            width: 1000px;
            margin: 10px auto;
            height: 150px;
            background-color: purple;
        }
        .banner{
            width: 1000px;
            margin: 10px auto;
            height: 250px;
            background-color: skyblue;
        }
        .main{
            width: 1000px;
            margin: 10px auto;
            height: 1000px;
            background-color: yellowgreen;
        }
        .slide-bar{
            width: 45px;
            height: 130px;
            background-color: pink;
            position: absolute;
            top: 300px;
            left: 50%;
            margin-left: 500px;
        }
        span{
            display: none;
            position: absolute;
            bottom: 0;
        }
    </style>
</head>
<body>
    <div class="header">头部区域</div>
    <div class="banner">banner区域</div>
    <div class="main">主体部分</div>
    <div class="slide-bar">
        <span class="goBack">返回顶部</span>
    </div>
    <script>
        //1. 获取元素
        var slidebar = document.querySelector('.slide-bar');
        var banner = document.querySelector('.banner');
        // banner.offestTop 就是被卷去头部的大小 一定要写到滚动的外面 
        var bannerTop = banner.offsetTop;
        // 当我们侧边栏固定定位之后应该变化的数值
        var slidebarTop = slidebar.offsetTop - bannerTop;
        // 获取main 主体元素
        var main = document.querySelector('.main');
        var goBack = document.querySelector('.goBack');
        var mainTop = main.offsetTop;
        document.addEventListener('scroll',function(){
            // window.pageYOffset 页面被卷去的头部
            // console.log(window.pageYOffset);
            // 3 .当我们页面被卷去的头部大于等于了 172 此时 侧边栏就要改为固定定位
            if(window.pageYOffset >= bannerTop){
                slidebar.style.position = 'fixed';
                slidebar.style.top = slidebarTop + 'px';
            }else{
                slidebar.style.position = 'absolute';
                slidebar.style.top = '300px';
            }
             // 4. 当我们页面滚动到main盒子，就显示 goback模块
             if(window.pageYOffset >= mainTop){
                goBack.style.display = 'block'
             }else{
                goBack.style.display = 'none'
             }

        })
    </script>
</body>
</html>